﻿using System;
using System.Drawing;
using System.Windows.Forms;
using VistaControls;
using VistaControls.TaskDialog;
using VistaControls.Dwm;

namespace PowerWaveAddin
{
    public partial class PublishStatusAeroForm : Form
    {
        /// <summary>
        /// Append event delegate and accessor
        /// </summary>
        /// <param name="str"></param>
        /// <param name="str2"></param>
        public delegate void AppendEventToAeroReport(string str, string str2);
        private AppendEventToAeroReport appendEventImpl;
        public AppendEventToAeroReport AppendStringToReportDelegateAccessor
        {
            get { return appendEventImpl; }
            private set { appendEventImpl = value; }
        }

        /// <summary>
        /// Update Progressbar percentage
        /// </summary>
        /// <param name="percentage"></param>
        public delegate void UpdateAeroProgressBarDelegate(int percentage);

        /// <summary>
        /// Update pewrcentage delegate wrapper
        /// </summary>
        public UpdateAeroProgressBarDelegate ProgressBarDelegateAccessor { get; private set; }


        public delegate void UpdateProgressBarStatusDelegate(VistaControls.ProgressBar.States ps);
        public UpdateProgressBarStatusDelegate UpdateProgressBarStatusDelegateAccessor { get; private set; }

        public delegate void UpdateLastEventStatusDelegate(string outcome);
        public UpdateLastEventStatusDelegate UpdateLastEventStatusDelegateAccessor { get; private set; }

        public delegate void PopErrorMessageDelegate(string errorText);
        public PopErrorMessageDelegate PopErrorMessageDelegateAccessor { get; private set; }

        private bool closeWhenDone = false;

        private System.Windows.Forms.ProgressBar progressBarToShow;

        /// <summary>
        /// Constructor
        /// </summary>
        public PublishStatusAeroForm()
        {
            InitializeComponent();
            
            ConvertToAeroMode();

            this.appendEventImpl = new AppendEventToAeroReport(AppendStringToReport);
            this.ProgressBarDelegateAccessor = new UpdateAeroProgressBarDelegate(UpdateStatusPercentage);
            this.UpdateProgressBarStatusDelegateAccessor = new UpdateProgressBarStatusDelegate(UpdateProgressBarStatus);
            this.UpdateLastEventStatusDelegateAccessor = new UpdateLastEventStatusDelegate(UpdateLastEventStatus);
            this.PopErrorMessageDelegateAccessor = new PopErrorMessageDelegate(PopUpError);
        }

        /// <summary>
        /// Set the status of the last event in the list to the given value
        /// </summary>
        /// <param name="outcome"></param>
        private void UpdateLastEventStatus(string outcome)
        {
            ListViewItem item = this.StatusListBox.Items[this.StatusListBox.Items.Count - 1];
            string mainValue = item.Text;
            item.SubItems.Clear();
            item.Text = mainValue;
            item.SubItems.Add(outcome);
            this.StatusListBox.Items[this.StatusListBox.Items.Count - 1] = item;
        }

        /// <summary>
        /// Updates the progressbar percentage.
        /// </summary>
        /// <param name="percent"></param>
        private void UpdateStatusPercentage(int percent)
        {
            this.progressBarToShow.Value = percent;
        }

        /// <summary>
        /// Changes the progressbar status if running in aero mode.
        /// </summary>
        /// <param name="ps"></param>
        private void UpdateProgressBarStatus(VistaControls.ProgressBar.States ps)
        {
            if (Aero.IsAeroSupported)
            {
                this.aeroProgressBar.ProgressState = ps;
            }
        }

        /// <summary>
        /// Adds a new line to the status list view.  If Aero is in use, the second string will be placed
        /// as a status description in the second column.
        /// </summary>
        /// <param name="str"></param>
        /// <param name="str2"></param>
        private void AppendStringToReport(string str, string str2)
        {
            ListViewItem item = new ListViewItem(str);
            item.SubItems.Add(str2);
            this.StatusListBox.Items.Add(item);
        }

        /// <summary>
        /// Pops up an error message using either Vista Aero or standard winforms depending on the system.
        /// </summary>
        /// <param name="errorText"></param>
        public void PopUpError(string errorText)
        {
            if (Aero.IsAeroSupported)
            {
                VistaControls.ProgressBar pb = this.progressBarToShow as VistaControls.ProgressBar;
                this.aeroProgressBar.SetState(VistaControls.ProgressBar.States.Error);

                TaskDialog.Show(
                    Properties.Resources.UploadExeptionMessage,
                    Properties.Resources.UploadExceptionCaption,
                    errorText,
                    TaskDialogButton.Close,
                    TaskDialogIcon.SecurityError);
            }
            else
            {
                MessageBox.Show(
                    errorText,
                    Properties.Resources.UploadExceptionCaption,
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
            }

            this.closeWhenDone = false;
        }

        private void PublishStatusAeroForm_Load(object sender, EventArgs e)
        {
        }

        /// <summary>
        /// Converts the controls on the form to their Aero equivalent if Aero is supported.
        /// </summary>
        private void ConvertToAeroMode()
        {
            // Enable Aero effects if Aero is enabled
            if (Aero.IsAeroSupported)
            {
                this.progressBarToShow = this.aeroProgressBar;
                this.ProgressBar.Visible = false;
                this.aeroProgressBar.Visible = true;

                this.BackColor = Color.Yellow;
                VistaControls.Dwm.DwmManager.EnableGlassFrame(this, new VistaControls.Dwm.Margins(50, 50, 50, 150));
            }
            else
            {
                this.progressBarToShow = this.ProgressBar;
                this.aeroProgressBar.Visible = false;
                this.ProgressBar.Visible = true;
            }
        }

        private void StatusListBox_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}